﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;//Please add references
using System.Collections.Generic;
namespace ZX.HIS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:RegistType
    /// </summary>
    public partial class RegistType : IRegistType
    {
        public RegistType()
        { }
        #region Depart自写方法
        /// <summary>
        /// 挂号项表添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int RegistTypelAdd(ZX.HIS.Model.RegistType model)
        {
            string str = "Proc_RegistTypelAdd";
            SqlParameter[] Parameter ={
                                      new SqlParameter("@HospitalId",SqlDbType.Int){Value=model.HospitalId},
                                      new SqlParameter("@RegistTypeName",SqlDbType.VarChar){Value=model.RegistTypeName},
                                      new SqlParameter("@RegistTypeCostPrice",SqlDbType.Decimal){Value=model.RegistTypeCostPrice},
                                      new SqlParameter("@RegistTypeSalePrice",SqlDbType.Decimal){Value=model.RegistTypeSalePrice},
                                      new SqlParameter("@RegistTypeState",SqlDbType.Int){Value=model.RegistTypeState}
                                    };
            object obj = SqlDBhelper.RunProcedureScalar(str, Parameter);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="StrOrder"></param>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public List<ZX.HIS.Model.RegistType> RegistTypeShow(int PageIndex, int PageSize, string RegistTypeName, int HospitalId)
        {
            StringBuilder str = new StringBuilder();
            str.AppendLine("SELECT * FROM(");
            str.AppendLine("SELECT * FROM(");
            str.AppendLine("SELECT ROW_NUMBER()OVER(");
            str.AppendLine("order by RegistTypeId)as b,tbRegistType.* from tbRegistType");
            str.AppendLine("where RegistTypeName like '%" + RegistTypeName + "%' ");
            str.AppendLine(" AND HospitalId=" + HospitalId);
            str.AppendLine(")TT");
            str.AppendLine("WHERE b  between  " + ((PageIndex - 1) * PageSize + 1) + " AND " + PageIndex * PageSize);
            str.AppendLine(")C");
            return SqlDBhelper.GetList<ZX.HIS.Model.RegistType>(str.ToString());

        }

        /// <summary>
        /// 查询总数量
        /// </summary>
        /// <param name="StrWher"></param>
        /// <returns></returns>
        public int RegistTypeCount(int HospitalId)
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT COUNT(*) FROM tbRegistType WHERE HospitalId= " + HospitalId);
            return SqlDBhelper.ExecuteScalarInt(str.ToString());
        }

        /// <summary>
        /// 伪删除挂号
        /// </summary>
        /// <param name="IDS"></param>
        /// <param name="DeparStarte"></param>
        /// <returns></returns>
        public int RegistTypeUpd(ZX.HIS.Model.RegistType mode)
        {
            StringBuilder str = new StringBuilder();
            str.Append("UPDATE tbRegistType SET RegistTypeState=" + mode.RegistTypeState + " WHERE RegistTypeId=" + mode.RegistTypeId + " ");
            int i = SqlDBhelper.ExecuteNonQuery(str.ToString());
            return i;
        }

        /// <summary>
        /// 挂号类型的所有显示
        /// </summary>
        /// <returns></returns>
        public List<ZX.HIS.Model.RegistType> RegistTypeList(int HospitalId, int RegistTypeId)
        {
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine(" SELECT RegistTypeId,HospitalId,RegistTypeName,RegistTypeCostPrice,RegistTypeSalePrice,RegistTypeState");
            SQL.AppendLine(" FROM tbRegistType WHERE 1=1");
            if (HospitalId != 0)
            {
                SQL.AppendLine(" AND HospitalId=" + HospitalId + "");
            }
            if (RegistTypeId != 0)
            {
                SQL.AppendLine(" AND RegistTypeId=" + RegistTypeId + "");
            }
            return SqlDBhelper.GetList<ZX.HIS.Model.RegistType>(SQL.ToString());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZX.HIS.Model.RegistType GetRegistTypeModel(int RegistTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RegistTypeId,HospitalId,RegistTypeName,RegistTypeCostPrice,RegistTypeSalePrice,RegistTypeState from tbRegistType ");
            strSql.Append(" where RegistTypeId=@RegistTypeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@RegistTypeId", SqlDbType.Int,4)
            };
            parameters[0].Value = RegistTypeId;
            ZX.HIS.Model.RegistType model = new ZX.HIS.Model.RegistType();
            DataSet ds = SqlDBhelper.DataSetQuery(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZX.HIS.Model.RegistType DataRowToModel(DataRow row)
        {
            ZX.HIS.Model.RegistType model = new ZX.HIS.Model.RegistType();
            if (row != null)
            {
                if (row["RegistTypeId"] != null && row["RegistTypeId"].ToString() != "")
                {
                    model.RegistTypeId = int.Parse(row["RegistTypeId"].ToString());
                }
                if (row["HospitalId"] != null && row["HospitalId"].ToString() != "")
                {
                    model.HospitalId = int.Parse(row["HospitalId"].ToString());
                }
                if (row["RegistTypeName"] != null)
                {
                    model.RegistTypeName = row["RegistTypeName"].ToString();
                }
                if (row["RegistTypeCostPrice"] != null && row["RegistTypeCostPrice"].ToString() != "")
                {
                    model.RegistTypeCostPrice = decimal.Parse(row["RegistTypeCostPrice"].ToString());
                }
                if (row["RegistTypeSalePrice"] != null && row["RegistTypeSalePrice"].ToString() != "")
                {
                    model.RegistTypeSalePrice = decimal.Parse(row["RegistTypeSalePrice"].ToString());
                }
                if (row["RegistTypeState"] != null && row["RegistTypeState"].ToString() != "")
                {
                    model.RegistTypeState = int.Parse(row["RegistTypeState"].ToString());
                }
            }
            return model;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="med"></param>
        /// <returns></returns>
        public int RegistTypeUpt(ZX.HIS.Model.RegistType med)
        {
            StringBuilder str = new StringBuilder();
            str.AppendLine("UPDATE tbRegistType SET");
            str.AppendLine("RegistTypeName=@RegistTypeName,");
            str.AppendLine("RegistTypeCostPrice=@RegistTypeCostPrice,");
            str.AppendLine("RegistTypeSalePrice=@RegistTypeSalePrice");
            str.AppendLine(" WHERE RegistTypeId=@RegistTypeId");
            SqlParameter[] P ={
                             new SqlParameter("@RegistTypeName",SqlDbType.VarChar),
                             new SqlParameter("@RegistTypeCostPrice",SqlDbType.Decimal),
                             new SqlParameter("@RegistTypeSalePrice",SqlDbType.Decimal),
                             new SqlParameter("@RegistTypeId",SqlDbType.Int)

                            };
            P[0].Value = med.RegistTypeName;
            P[1].Value = med.RegistTypeCostPrice;
            P[2].Value = med.RegistTypeSalePrice;
            P[3].Value = med.RegistTypeId;

            return SqlDBhelper.ExecuteNonQuery(str.ToString(), P);

        }

        public List<ZX.HIS.Model.RegistType> list()
        {
            string sql = "select * from tbRegistType";
            return SqlDBhelper.GetList<ZX.HIS.Model.RegistType>(sql);
        }
        #endregion
        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("RegistTypeId", "tbRegistType"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int RegistTypeId)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) from tbRegistType");
        //    strSql.Append(" where RegistTypeId=@RegistTypeId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@RegistTypeId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = RegistTypeId;

        //    return DbHelperSQL.Exists(strSql.ToString(),parameters);
        //}


        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.RegistType model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("insert into tbRegistType(");
        //    strSql.Append("HospitalId,RegistTypeName,RegistTypeCostPrice,RegistTypeSalePrice,RegistTypeState)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalId,@RegistTypeName,@RegistTypeCostPrice,@RegistTypeSalePrice,@RegistTypeState)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@RegistTypeName", SqlDbType.VarChar,50),
        //            new SqlParameter("@RegistTypeCostPrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@RegistTypeSalePrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@RegistTypeState", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.RegistTypeName;
        //    parameters[2].Value = model.RegistTypeCostPrice;
        //    parameters[3].Value = model.RegistTypeSalePrice;
        //    parameters[4].Value = model.RegistTypeState;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.RegistType model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbRegistType set ");
        //    strSql.Append("HospitalId=@HospitalId,");
        //    strSql.Append("RegistTypeName=@RegistTypeName,");
        //    strSql.Append("RegistTypeCostPrice=@RegistTypeCostPrice,");
        //    strSql.Append("RegistTypeSalePrice=@RegistTypeSalePrice,");
        //    strSql.Append("RegistTypeState=@RegistTypeState");
        //    strSql.Append(" where RegistTypeId=@RegistTypeId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@RegistTypeName", SqlDbType.VarChar,50),
        //            new SqlParameter("@RegistTypeCostPrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@RegistTypeSalePrice", SqlDbType.Decimal,9),
        //            new SqlParameter("@RegistTypeState", SqlDbType.Int,4),
        //            new SqlParameter("@RegistTypeId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.RegistTypeName;
        //    parameters[2].Value = model.RegistTypeCostPrice;
        //    parameters[3].Value = model.RegistTypeSalePrice;
        //    parameters[4].Value = model.RegistTypeState;
        //    parameters[5].Value = model.RegistTypeId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int RegistTypeId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbRegistType ");
        //    strSql.Append(" where RegistTypeId=@RegistTypeId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@RegistTypeId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = RegistTypeId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string RegistTypeIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbRegistType ");
        //    strSql.Append(" where RegistTypeId in ("+RegistTypeIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}



        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select RegistTypeId,HospitalId,RegistTypeName,RegistTypeCostPrice,RegistTypeSalePrice,RegistTypeState ");
        //    strSql.Append(" FROM tbRegistType ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" RegistTypeId,HospitalId,RegistTypeName,RegistTypeCostPrice,RegistTypeSalePrice,RegistTypeState ");
        //    strSql.Append(" FROM tbRegistType ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbRegistType ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.RegistTypeId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbRegistType T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbRegistType";
        //    parameters[1].Value = "RegistTypeId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

